<?php
namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\ExperienceModel;

class ExperienceController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    /**
     * 发布心得
     */
    public function add_experience(Request $request,ExperienceModel $experienceModel){

      //  DB::beginTransaction();
        try{
            //验证规则
            $rule = [
                //'goods_id' => 'required|exists:goods,id',
                'title' => 'required',
                'country' => 'required',
                'line_id' => 'required',
                'line' => 'required',
                'country_id' => 'required',
                'city_id' => 'required',
                'itinerary_date' => 'required',
                'route_line' => 'required|array',
                'route_line.*.time' => 'required',
                'route_line.*.line' => 'required|distinct',
                'route_line.*.content' => 'required|distinct',

            ];

            // 验证信息
            $message = [

                'title.required' => '标题必传',
                'country.required' => '国家名称必传',
                'line_id.required' => '线路类型ID不能为空',
                'line.required' => '线路类型不能为空',
                'country_id.required' => '国家ID不为空',
                'city_id.required' => '城市ID不为空',
                'itinerary_date.required' => '行程时间不为空',
                'route_line.required' => 'route_line不为空',
                'route_line.array' => 'route_line格式为数组',
                'route_line.*.time.required' => '行程线路-时间不能为空',
                'route_line.*.line.required' => '行程线路-行程线路不能为空',
                'route_line.*.content.required' => '行程线路-内容不能为空',
                //'order_info.*.time.distinct' => '行程线路-时间不能重复',
                'order_info.*.line.distinct' => '行程线路-行程线路不能重复',
                'order_info.*.content.required' => '行程线路-内容不能重复',
                //'order_info.*.passport.required' => '登记人护照不能为空',
            ];

            $validator = Validator::make($request->all(), $rule, $message);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $experienceModel->title = $request->input('title');
            $experienceModel->country = $request->input('country');
            $experienceModel->line_id = $request->input('line_id');
            $experienceModel->line = $request->input('line');
            $experienceModel->country_id = $request->input('country_id');
            $experienceModel->route_line = $request->input('route_line');
            $experienceModel->html = $request->input('html');
            $experienceModel->user_id = auth('user_api') -> user()->id;
            $experienceModel->user_name  = auth('user_api') -> user()->nickName;
            $experienceModel->head_img = auth('user_api') -> user()->head_img;


            $res= $experienceModel->save();
            if(!$res){
                return returnMsg(400,'保存失败');
            }

            return returnMsg(0,'发布成功');

          //  DB::commit();

        }catch (\Exception $e){
            //DB::rollBack();
            return returnMsg(400,$e -> getMessage());
        }

    }


    //心得列表
    public function experience_list(Request $request,ExperienceModel $experienceModel){

        $query = $experienceModel->select(['*']);

        if(!empty($request->input('country_id'))){
            $query->where('country_id',$request->input('country_id'));
        }

        if(!empty($request->input('city_id'))){
            $query->where('city_id',$request->input('city_id'));
        }

        if(!empty($request->input('line_id'))){
            $query->where('country_id',$request->input('line_id'));
        }

        if(!empty($request->input('keyword'))){
            $query->where('title','like','%'.$request->input('keyword').'%');
        }

        $data = $query->paginate(15);

        return returnMsg(0,'获取成功',$data);

    }

    //查询我的心得
    public function my_experience(ExperienceModel $experienceModel){

        $data = $experienceModel
            ->select([
                'id',
                'title',
                'country',
                'line',
                'user_name',
                'user_id',
                'head_img',
                'html',
                'created_at',
            ])
            ->where('user_id',auth('user_api') -> user()->id)
            ->paginate(15);

        return returnMsg(0,'获取成功',$data);

    }

    //心得详情
    public function experience_info(Request $request,ExperienceModel $experienceModel){

        $data = $experienceModel->where('id',$request->input('experience_id'))->first();

        return returnMsg(0,'获取成功',$data);

    }

    //删除我的心得
    public function del_my_experience(Request $request,ExperienceModel $experienceModel){

        $id= $request->input('experience_id');

        $res = $experienceModel->where('id',$id)->delete();

        if(!$res){
            return returnMsg(400,'更改失败');
        }

        return returnMsg(0,'更改成功');
    }



}